

//
//  HMEmotionToolbar.m
//  黑马微博-wzy
//
//  Created by Mac on 2017/2/13.
//  Copyright © 2017年 Mac. All rights reserved.
//

#import "HMEmotionToolbar.h"


#define HMEmotionButtonMaxCount 4

@interface HMEmotionToolbar()

//默认选中按钮
//@property (nonatomic, strong) UIButton *defaultButton;

//记录上次选中的按钮
@property (nonatomic, strong) UIButton *seletedButton;

@end


@implementation HMEmotionToolbar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //1.添加 工具条上的按钮
        [self setupToolBarButton:@"最近" tag:HMEmotionTypeRecent];
        [self setupToolBarButton:@"默认" tag:HMEmotionTypeDefault];
        [self setupToolBarButton:@"Emoji" tag:HMEmotionTypeEmoji];
        [self setupToolBarButton:@"浪小花" tag:HMEmotionTypeLxh];
        
        //2.默认选中"默认" 按钮
//        [self toolbarButtonClick:self.defaultButton];
    }
    return self;
}

//设置 toolbarButton
- (UIButton *)setupToolBarButton:(NSString *)title tag:(HMEmotionType)tag{
    //1.创建按钮
    UIButton *button = [[UIButton alloc] init];
    button.tag = tag;
    
    //2.设置按钮文字
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateSelected];
    [button addTarget:self action:@selector(toolbarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    button.titleLabel.font = [UIFont systemFontOfSize:13];
    [self addSubview:button];
    
    
    //3.注意 左中右 按钮的背景图片是有 区别的
    //因为 添加按钮的时候是有顺序的,从左到右,所以可以根据当前self.toolbar.subviews.count来判断,现在添加的按钮的位置
    int count = (int)self.subviews.count;
    if (count == 1) {
        //第一个按钮
        [button setBackgroundImage:[UIImage resizedImage:@"compose_emotion_table_left_normal"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage resizedImage:@"compose_emotion_table_left_selected"] forState:UIControlStateSelected];
    }else if(count == HMEmotionButtonMaxCount){
        //最后一个按钮
        [button setBackgroundImage:[UIImage resizedImage:@"compose_emotion_table_right_normal"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage resizedImage:@"compose_emotion_table_right_selected"] forState:UIControlStateSelected];
        
    }else{
        //中间的按钮
        [button setBackgroundImage:[UIImage resizedImage:@"compose_emotion_table_mid_normal"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage resizedImage:@"compose_emotion_table_mid_selected"] forState:UIControlStateSelected];
        
    }
    return button;
}

//监听 toolbarButtonClick
- (void)toolbarButtonClick:(UIButton *)button{
    //1.把上次选中按钮的 选中状态 为 NO
    self.seletedButton.selected = NO;
    //2.设置当前选中按钮 选中状态 为YES
    button.selected = YES;
    //3.记录下当前选中的按钮
    self.seletedButton = button;
    
    //4.通知代理
    if ([self.delegate respondsToSelector:@selector(emotionToolbar:didSeletedButton:)]) {
        //
        [self.delegate emotionToolbar:self didSeletedButton:button.tag];
    }
}

//设置默认选中按钮
//重写代理的setter 方法
- (void)setDelegate:(id<HMEmotionToolbarDelegate>)delegate{

    _delegate = delegate;
    //获取默认按钮
    UIButton *defaultButton = (UIButton *)[self viewWithTag:HMEmotionTypeDefault];
    //默认选中 "默认" 按钮
    [self toolbarButtonClick:defaultButton];

}

- (void)layoutSubviews{
    [super layoutSubviews];
    //1.设置工具条上按钮的frame
    CGFloat btnW = kScreenWidth / HMEmotionButtonMaxCount;
    CGFloat btnH = self.height;
    for (int i = 0; i < HMEmotionButtonMaxCount; i++) {
        UIButton *button = self.subviews[i];
        button.width = btnW;
        button.height = btnH;
        button.x = i * btnW;
        button.y = 0;
    }


}


@end
